<template>
  <el-dialog v-model="roleVisible" title="修改角色名称">
    <el-form :model="props.roleForm" :rules="rules" ref="roleRef">
      <el-form-item label="标签名称" prop="role_name">
        <el-input v-model="props.roleForm.role_name" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="roleVisibleController(false)">取消</el-button>
        <el-button type="primary" @click="onSubmitUpdate(roleRef)"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" name="labelDialog" setup>
  import { reactive, ref } from 'vue'
  import type { FormInstance, FormRules } from 'element-plus'
  import { updateRoleNameRequest } from '@/network/api/rights-api'
  import notification from '@/utils/notification'
  let roleVisible = ref(false)
  const roleRef = ref<FormInstance>()
  const rules = reactive<FormRules>({
    role_name: [
      { required: true, message: '角色名称为必填项~', trigger: 'blur' },
      { min: 2, max: 15, message: '角色名称长度在2-15个字符之间~', trigger: 'blur' },
    ],
  })
  interface IRoleForm {
    id: number
    role_name: string
  }
  const props = defineProps<{
    roleForm: IRoleForm
  }>()
  const onSubmitUpdate = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate(async valid => {
      if (valid) {
        const result = await updateRoleNameRequest(props.roleForm.id, props.roleForm.role_name)
        roleVisibleController(false)
        notification.success(result.msg)
      } else {
        return
      }
    })
  }
  const roleVisibleController = (isClose: boolean) => {
    if (isClose) {
      roleVisible.value = true
    } else {
      roleVisible.value = false
    }
  }
  defineExpose({ roleVisibleController })
</script>

<style scoped></style>
